<template>
  <div class="goods-comment" v-if="commentInfo">
    <div class="head">
      <div class="data">
        <p><span>{{commentInfo.salesCount}}</span><span>人购买</span></p>
        <p><span>{{commentInfo.praisePercent}}</span><span>好评率</span></p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a href="javascript:;" @click="changeSort(tag.title) " :class="{active:activeTitle===tag.title}" v-for="tag in commentInfo.tags" :key="tag.title">{{tag.title }} ({{tag.tagCount}})</a>
        </div>
      </div>
    </div>
    <div class="sort">
      <span>排序：</span>
     <a
        @click="data.sortField = null"
        :class="{ active: data.sortField === null }"
        href="javascript:;"
      >
        默认
      </a>
      <a
        @click="data.sortField = 'createTime'"
        :class="{active:data.sortField ==='createTime'}"
        href="javascript:;"
      >
        最新
      </a>
      <a
        @click="data.sortField = 'praiseCount'"
        :class="{active:data.sortField ==='praiseCount'}"
        href="javascript:;"
      >
        最热
      </a>
    </div>
     <!-- 列表 -->
    <div class="list">
      <div class="item" v-for="item,index in commentList" :key="index">
        <div class="user">
          <img :src="item.member.avatar" alt="">
          <span>{{nicknamehide(item.member.nickname) }}</span>
        </div>
        <div class="body">
          <div class="score">
            <i class="iconfont icon-wjx01" v-for="i in item.score" :key="i"></i>

            <i class="iconfont icon-wjx02"  v-for="i in 5-item.score" :key="i"></i>
            <span class="attr" :key="index" v-for=" i,index in item.orderInfo.specs">{{i.name}}:{{i.nameValue}}</span>
          </div>
          <div class="text">{{item.content}}</div>
          <GoodsCommentImage v-if="item.pictures.length"  :pictures="item.pictures" />
          <div class="time">
            <span>{{item.createTime}}</span>
            <span class="zan"><i class="iconfont icon-dianzan"></i>{{item.praiseCount}}</span>
          </div>
        </div>
      </div>
    </div>
    <!-- 分页组件 -->
    <XtxPagination :pageSize='pageSize' :total='total'  @current-change="getCurrentPage"/>
  </div>
</template>

<script>
import { findCommentInfoByGoods, findGoodsCommentList } from '@/api/goods'
import GoodsCommentImage from '@/views/goods/components/goods-comment-image.vue'
import { inject, ref, reactive, watch } from 'vue'
export default {
  name: 'GoodsComment',
  components: { GoodsCommentImage },
  setup () {
    const goods = inject('goods')
    const commentInfo = ref(null)
    // 头部
    const activeTitle = ref('全部评价')
    findCommentInfoByGoods(goods).then(({ result }) => {
      result.tags = [
        { title: '全部评价', tagCount: result.evaluateCount },
        { title: '有图', tagCount: result.hasPictureCount },
        ...result.tags
      ]
      commentInfo.value = result
    })
    // 头部
    // 评论
    const commentList = ref([])
    const total = ref(0)
    const pageSize = ref(0)
    // 当前页码
    const currentPage = ref(1)
    const data = reactive({
      // page: 1, // 是 1 页码
      pageSize: 10, // 是 10 页尺寸
      hasPicture: '', // 否 true 是否有图
      tag: '', // 否 质量好 标签
      sortField: '', // 否 praiseCount 排序字段，可选值范围[praiseCount,createTime]
      sortMethod: '' // 否 desc 排序方法，可选值范围[asc,desc],默认为desc
    })
    const getList = () => {
      findGoodsCommentList(goods.id, { ...data, page: currentPage.value }).then(
        ({ result }) => {
          commentList.value = result.items
          total.value = result.counts
          pageSize.value = result.pageSize
        })
    }
    watch(data, () => {
      currentPage.value = 1
      getList()
    }, { immediate: true })
    const nicknamehide = (nickname) => {
      return nickname.slice(0, 1) + '****' + nickname.slice(-1)
    }
    const getCurrentPage = (val) => {
      currentPage.value = val
      getList()
    }
    const changeSort = (tag) => {
      activeTitle.value = tag
      if (tag === ' 有图') {
        data.hasPicture = true
        data.tag = null
      } else if (tag === ' 全部评价') {
        data.hasPicture = null
        data.tag = null
      } else {
        data.hasPicture = null
        data.tag = tag
      }
    }
    data.sortField = null
    // 评论
    return { goods, commentInfo, activeTitle, commentList, nicknamehide, data, total, pageSize, getCurrentPage, changeSort }
  }
}
</script>

<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      width: 340px;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;
      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }
      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        > a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;
          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor, 50%);
            color: @xtxColor;
          }
          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }
   .list {
    padding: 0 20px;
    .item {
      display: flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;
      .user {
        width: 160px;
        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }
        span {
          padding-left: 10px;
          color: #666;
        }
      }
      .body {
        flex: 1;
        .score {
          line-height: 40px;
          .iconfont {
            color: #ff9240;
            padding-right: 3px;
          }
          .attr {
            padding-left: 10px;
            color: #666;
          }
        }
      }
      .text {
        color: #666;
        line-height: 24px;
      }
      .time {
        color: #999;
        display: flex;
        justify-content: space-between;
        margin-top: 5px;
      }
    }
  }
}
</style>
